package com.tl.common.view;

import java.util.List;

import com.tl.tlcommon.R;

import android.R.color;
import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MyTabViewGroup extends HorizontalScrollView {

	private Context mContext;
	RadioButton[] btns = null;
	RadioButton selectBtn = null;
	int selectPostion = 0;
	RadioGroup group;
	LinearLayout tabLayout;

	int mMaxTabWidth;
	private int mContentHeight;

	public MyTabViewGroup(Context context) {
		super(context);
		this.mContext = context;
		setHorizontalScrollBarEnabled(false);
		initTabView();
	}

	public MyTabViewGroup(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
		setHorizontalScrollBarEnabled(false);
		initTabView();
	}

	private void initTabView() {
		tabLayout = new LinearLayout(getContext());
		tabLayout.setBackgroundResource(R.drawable.abs__tab_uncheck_bg);
		tabLayout.setOrientation(LinearLayout.HORIZONTAL);
		tabLayout.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.MATCH_PARENT));

		addView(tabLayout, new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.MATCH_PARENT));
	}

	public void setDatas(List<String> datas) {
		if (datas == null || datas.size() <= 0)
			return;

		btns = new RadioButton[datas.size()];
		for (int i = 0; i < datas.size() - 1; i++) {
			final String text = datas.get(i);
			RadioButton btn = createButton(text, i);

			btns[i] = btn;
			addTab(btn, false);
			requestLayout();
		}
		RadioButton btn = createButton(datas.get(datas.size() - 1),
				datas.size() - 1);
		btns[datas.size() - 1] = btn;
		addTab(btn, true);

		// selectBtn = btns[0];
		// selectPostion = 0;
		// selectBtn.setChecked(true);
		requestLayout();
	}

	private RadioButton createButton(String text, final int position) {
		RadioButton btn = (RadioButton) LayoutInflater.from(getContext())
				.inflate(R.layout.tabbuttonview, null);
		btn.setText(text);
		btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					setSelectButton(position);
				}

			}
		});

		RadioGroup.LayoutParams btnParam = new RadioGroup.LayoutParams(0,
				LayoutParams.MATCH_PARENT, 1);
		btn.setLayoutParams(btnParam);
		return btn;
	}

	/**
	 * 设置选择的button
	 * 
	 * @param item
	 */
	public void setSelectButton(String item) {
		if (btns == null || item == null || "".equals(item))
			return;
		for (int i = 0; i < btns.length; i++) {
			RadioButton btn = btns[i];
			if (item.equals(btn.getText().toString()))
				setSelectButton(i);
		}
	}

	/**
	 * 设置选择的button
	 * 
	 * @param position
	 */
	public void setSelectButton(int position) {
		if (btns == null)
			return;
		for (int i = 0; i < btns.length; i++) {
			RadioButton btn = btns[i];
			btn.setChecked(false);
			// btn.setTextColor(color.white);
		}
		selectPostion = position;
		selectBtn = btns[position];
		selectBtn.setChecked(true);
		TabView tabview = (TabView) tabLayout.getChildAt(position);

		final int scrollPos = tabview.getLeft()
				- (getWidth() - tabview.getWidth()) / 2;
		smoothScrollTo(scrollPos, 0);

		changeListener.onSelectChangedLister(position);
	}

	/**
	 * 获取当前选择的button
	 * 
	 * @return
	 */
	public RadioButton getSelectButton() {
		return selectBtn;
	}

	private onSelectChangedListener changeListener;

	public void setOnSeleteChangedListener(onSelectChangedListener listener) {
		this.changeListener = listener;
	}

	public interface onSelectChangedListener {
		abstract void onSelectChangedLister(int position);
	}

	/**
	 * 获取当前选择的位置
	 * 
	 * @return
	 */
	public int getSelectPostion() {
		return selectPostion;
	}

	private TabView createTabView(RadioButton btn, boolean isLast) {
		// Workaround for not being able to pass a defStyle on pre-3.0
		TabView tabView = new TabView(getContext(), null);
		tabView.init(btn, isLast);

		return tabView;
	}

	public void addTab(RadioButton btn, boolean isLast) {
		TabView tab = createTabView(btn, isLast);
		// tab.setPadding(0, 0, 0, 0);
		LayoutParams blp = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT, 1);
		tab.setLayoutParams(blp);
		tabLayout.addView(tab, new LinearLayout.LayoutParams(0,
				LayoutParams.MATCH_PARENT, 1));
	}

	@Override
	public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		final boolean lockedExpanded = widthMode == MeasureSpec.EXACTLY;
		setFillViewport(lockedExpanded);

		final int childCount = tabLayout.getChildCount();
		if (childCount > 1
				&& (widthMode == MeasureSpec.EXACTLY || widthMode == MeasureSpec.AT_MOST)) {
			if (childCount > 2) {
				mMaxTabWidth = (int) (MeasureSpec.getSize(widthMeasureSpec) * 0.4f);
			} else {
				mMaxTabWidth = MeasureSpec.getSize(widthMeasureSpec) / 2;
			}
		} else {
			mMaxTabWidth = -1;
		}

		// heightMeasureSpec = MeasureSpec.makeMeasureSpec(mContentHeight,
		// MeasureSpec.EXACTLY);

		final int oldWidth = getMeasuredWidth();
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		final int newWidth = getMeasuredWidth();

		if (lockedExpanded && oldWidth != newWidth) {
			// Recenter the tab display if we're at a new (scrollable) size.
			setSelectButton(selectPostion);
		}
	}

	class TabView extends LinearLayout {

		private RadioButton btn;
		private boolean isLast;

		public TabView(Context context, AttributeSet attrs) {
			// TODO super(context, null, R.attr.actionBarTabStyle);
			super(context, attrs);
		}

		public void init(RadioButton btn, Boolean isLast) {
			this.btn = btn;
			this.isLast = isLast;
			update();
		}

		public void update() {
			if (!isLast) {
				ImageView iconView = new ImageView(getContext());
				LayoutParams lp = new LayoutParams(5, LayoutParams.MATCH_PARENT);
				lp.gravity = Gravity.CENTER_VERTICAL;
				iconView.setLayoutParams(lp);
				iconView.setPadding(4, 4, 4, 4);
				addView(iconView);
				iconView.setBackgroundResource(R.drawable.tab_divider_bg);
				iconView.setVisibility(VISIBLE);
			}

			TextView textView = new TextView(getContext());
			textView.setEllipsize(TruncateAt.END);
			LayoutParams blp = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1);
			blp.gravity = Gravity.CENTER_VERTICAL;
			textView.setLayoutParams(blp);
			addView(btn, 0);

		}

		public RadioButton getRadioButtonView() {
			return btn;
		}

	}

}
